﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using SkypeBot.Core;

namespace SkypeBot.Application
{
    using Core = SkypeBot.Core.Core;

    public class SkypeBotForm:Form
    {
        private Dictionary<string, string> _ReciverDictionary = new Dictionary<string, string>();

        [STAThread]
        public static void Main()
        {
            System.Windows.Forms.Application.Run(new SkypeBotForm());
        }

        private NotifyIcon objTrayIcon;
        private ContextMenu objTrayMenu;

        public SkypeBotForm()
        {
            Core.Instance.StartBot();
            ConsoleHelper.ShowConsoleWindow();
            // Create a simple tray menu with only one item.
            objTrayMenu = new ContextMenu();
            objTrayMenu.MenuItems.Add(new MenuItem("LaunchMode"));
            MenuItem mobjDevRadio = new MenuItem("Development",OnLaunchModeChange);
            mobjDevRadio.RadioCheck = true;
            mobjDevRadio.Checked = true;
            MenuItem mobjProdRadio = new MenuItem("Production",OnLaunchModeChange);
            mobjProdRadio.RadioCheck = true;
            objTrayMenu.MenuItems[0].MenuItems.Add(mobjDevRadio);
            objTrayMenu.MenuItems[0].MenuItems.Add(mobjProdRadio);
            objTrayMenu.MenuItems.Add("-");
            objTrayMenu.MenuItems.Add(new MenuItem("Receivers"));
            objTrayMenu.MenuItems[2].MenuItems.Add(new MenuItem("Users"));
            objTrayMenu.MenuItems[2].MenuItems.Add(new MenuItem("Chats"));
            foreach (var user in Core.Instance.Users)
            {
                SKYPE4COMLib.User mobjUser = user as SKYPE4COMLib.User;
                string strFullName = mobjUser.FullName;
                if (string.IsNullOrEmpty(strFullName))
                {
                    strFullName = mobjUser.Handle;
                }
                MenuItem mobjMenuItem = new MenuItem(strFullName, OnCheckItem);
                mobjMenuItem.Checked = false;
                objTrayMenu.MenuItems[2].MenuItems[0].MenuItems.Add(mobjMenuItem);
                _ReciverDictionary.Add(strFullName, mobjUser.Handle);
            }
            foreach (var chat in Core.Instance.Chats)
            {
                SKYPE4COMLib.Chat mobjChat = chat as SKYPE4COMLib.Chat;
                DateTime mobjActivityStamp = mobjChat.ActivityTimestamp;
                if (mobjActivityStamp >= DateTime.Now.AddMonths(-1) && mobjChat.Members.Count > 2)
                { 
                    string strChatName = "Chat:" + mobjChat.FriendlyName;
                    MenuItem mobjMenuItem = new MenuItem(strChatName, OnCheckItem);
                    mobjMenuItem.Checked = false;
                    objTrayMenu.MenuItems[2].MenuItems[1].MenuItems.Add(mobjMenuItem);
                    _ReciverDictionary.Add(strChatName, mobjChat.Name);
                }
            }
            objTrayMenu.MenuItems.Add("-");
            objTrayMenu.MenuItems.Add("Exit", OnExit);

            // Create a tray icon. In this example we use a
            // standard system icon for simplicity, but you
            // can of course use your own custom icon too.
            objTrayIcon = new NotifyIcon();
            objTrayIcon.Text = "SkypeBot";
            Bitmap mobjBitMap = Properties.Resources.SkypeBotIcon; 
            IntPtr mobjHicon = mobjBitMap.GetHicon();
            Icon mobjIcon = Icon.FromHandle(mobjHicon);
            objTrayIcon.Icon = mobjIcon;

            // Add menu to tray icon and show it.
            objTrayIcon.ContextMenu = objTrayMenu;
            objTrayIcon.Visible = true;
        }

        private void OnLaunchModeChange(object sender, EventArgs e)
        {
            MenuItem mobjItem = sender as MenuItem;
            if (!mobjItem.Checked)
            {
                MenuItem mobjParentItem = mobjItem.Parent as MenuItem;
                int intIndex = mobjParentItem.MenuItems.IndexOf(mobjItem);
                mobjItem.Checked = true;
                int intAnotherIndex = intIndex == 0 ? 1 : 0;
                mobjParentItem.MenuItems[intAnotherIndex].Checked = false;
                if (mobjParentItem.MenuItems[0].Checked)
                {
                    Core.Instance.CurrentLaunchMode = Core.LaunchMode.Dev;
                    ConsoleHelper.ShowConsoleWindow();
                }
                else { Core.Instance.CurrentLaunchMode = Core.LaunchMode.Prod; ConsoleHelper.HideConsoleWindow(); }
            }
        }

        private void OnCheckItem(object sender, EventArgs e)
        {
            MenuItem mobjItem = sender as MenuItem;
            mobjItem.Checked = !mobjItem.Checked;
            foreach(var item in _ReciverDictionary)
            {
                if(item.Key == mobjItem.Text)
                {
                    bool IsChat = false;
                    if(item.Key.StartsWith("Chat"))
                    {
                        IsChat = true;
                    }
                    if (mobjItem.Checked)
                    {
                        Core.Instance.Receivers.Add(new Receiver(item.Value, IsChat));
                    }
                    else 
                    {
                        Receiver mobjReceiver = null;
                        foreach (var receiver in Core.Instance.Receivers)
                        {
                            if (receiver.ReceiverName == item.Value)
                            {
                                mobjReceiver = receiver;  
                            }
                        }
                        if (mobjReceiver != null)
                        {
                            Core.Instance.Receivers.Remove(mobjReceiver);
                        }
                    }
                    
                }
            }
            
        }

        private void OnExit(object sender, EventArgs e)
        {
            Core.Instance.StopBot();
            System.Windows.Forms.Application.Exit();
        }


        protected override void OnLoad(EventArgs e)
        {
            Visible = false; // Hide form window.
            ShowInTaskbar = false; // Remove from taskbar.

            base.OnLoad(e);
        }

        protected override void Dispose(bool isDisposing)
        {
            if (isDisposing)
            {
                // Release the icon resource.
                objTrayIcon.Dispose();
            }

            base.Dispose(isDisposing);
        }

    }
}
